package com.neuedu.corejava.ch13.homework;

public class ThreadDemo {
	private int i = 0;

	private synchronized void inc() {
		i++;
		// currentThread返回对当前正在执行的线程对象的引用
		System.out.println(Thread.currentThread().getName() + "-执行加的操作-结果：" + i);
	}

	private synchronized void dec() {
		i--;
		System.out.println(Thread.currentThread().getName() + "-执行减的操作-结果：" + i);
	}

	class Inc implements Runnable {

		@Override
		public void run() {
			inc();
		}

	}

	class Dec implements Runnable {

		@Override
		public void run() {
			dec();
		}

	}

	public static void main(String[] args) {
		ThreadDemo demo = new ThreadDemo();
		ThreadDemo.Inc inc = demo.new Inc();
		ThreadDemo.Dec dec = demo.new Dec();

		for (int i = 0; i < 10; i++) {
			new Thread(inc).start();
			new Thread(dec).start();
		}
	}

}
